cd "Z:\PUBLIC_web\Stataworkshops\Grammar of Graphics\Graphical objects" sysuse auto, clear generate maker = substr(make, 1, strpos(make, " ")-1) replace maker = make if strpos(make, " ")==0 label variable maker "Manufacturer" * Two families of geometric objects, one family for continuous by continuous, * another family for categorical by continuous * Continuous by continuous * Objects anchored by single (x,y) pairs * Points scatter mpg weight, title("scatter") name(g1) * Line segments sort weight mpg // usually used with ordered data line mpg weight, title("line") name(g2) * Line segments AND points twoway connected mpg weight, title("connected") name(g3) scatter mpg weight, connect(l) // internally, the same as "connected" // overlay, different color ink twoway (scatter mpg weight) (line mpg weight), title("scatter || line") name(g4) graph combine g1 g2 g3 g4, title("Anchored by (x,y) points") graph export "points.png", replace graph drop _all * Objects anchored by arbitrary (x,y) points, and the x axis * Bars * perhaps most useful as a programmer's tool, for use with data * already in summary form, and/or in combination with other * "twoway" geometrical objects. twoway bar mpg weight, title("bar") name(g1) // first glance // relation to scatter twoway (bar mpg weight) (scatter mpg weight), title("bar || scatter") name(g2) gsort - mpg weight // to better see overlay/collision of bars twoway bar mpg weight, title("sorted to see overlay") name(g3) graph combine g1 g2 g3, title("Bars connecting x axis to points") graph export "bars.png", replace * similar to bars twoway spike mpg weight, title("spike") name(g4) // "spike" is to "bar" as "line" is to "scatter" twoway dropline mpg weight, title("dropline") name(g5) // like "connected" twoway (spike mpg weight) (scatter mpg weight), title("spike || scatter") name(g6) // two color graph combine g1 g4 g5 g6, title("Lines connecting x axis to points") graph export "spikes.png", replace graph drop _all * Areas sort weight mpg // usually used with ordered data twoway area mpg weight, title("area") name(g1) twoway (area mpg weight) (connected mpg weight), /// legend(off) title("area || connected") name(g2) graph combine g1 g2, title("Areas connecting x axis to points") graph export "areas.png", replace graph drop _all * Dots * like scatter, but with vertical guide lines. twoway dot mpg weight, title("dot") name(g1) // perhaps useful with nested data, or // combined with something else? // for example preserve collapse (mean) mpgm=mpg (semean) mpgse=mpg, by(rep78) gen lcl = mpgm - 1.96*mpgse gen ucl = mpgm + 1.96*mpgse twoway (rspike lcl ucl rep78) (dot mpgm rep78), /// ylabel(0(5000)16000) pcycle(1) title("rspike || dot") name(g2) restore graph combine g1 g2, title("twoway dot") graph export "twoway dots.png", replace graph drop _all * Objects anchored by an aligned pair of points: (x, y1) and (x, y2) * mostly the same geometric objects as before preserve collapse (min) mpgmin=mpg (max) mpgmax=mpg, by(weight) twoway rscatter mpgmin mpgmax weight, title("rscatter") name(g1) scatter mpgmin mpgmax weight, title("scatter || scatter") name(g2) // another way to acheive this, layers colored twoway rline mpgmin mpgmax weight, title("rline") name(g3) twoway rconnected mpgmin mpgmax weight, title("rconnected") name(g4) graph combine g1 g2 g3 g4, title("Ranges by endpoints") graph export "range endpoints.png", replace twoway rbar mpgmin mpgmax weight, title("rbar") name(g5) twoway rspike mpgmin mpgmax weight, title("rspike") name(g6) twoway rcap mpgmin mpgmax weight, title("rcap") name(g7) // and rcapsym, more flexible than dropline twoway rarea mpgmin mpgmax weight, title("rarea") name(g8) graph combine g5 g6 g7 g8, title("Ranges by connected endpoints") graph export "range intervals.png", replace // naturally you could overlay these twoway (rline mpgmin mpgmax weight) /// (rspike mpgmin mpgmax weight), title("rline || rspike") graph export "range overlay.png", replace restore graph drop _all * Objects anchored by an arbitrary pair of points: (x1, y1) and (x2, y2) * points and line segements of various types, no areas (polygons?) preserve collapse mpg weight, by(rep78 foreign) reshape wide mpg weight, i(rep78) j(foreign) twoway pcspike mpg0 weight0 mpg1 weight1, title("pcspike") name(g1) twoway pcscatter mpg0 weight0 mpg1 weight1, mlabel(rep78) title("pcscatter") name(g2) twoway pccapsym mpg0 weight0 mpg1 weight1, mlabel(rep78) title("pccapsym") name(g3) twoway pcarrow mpg0 weight0 mpg1 weight1, title("pcarrow") name(g4) twoway pcbarrow mpg0 weight0 mpg1 weight1, title("pcbarrow") name(g5) twoway (pccapsym mpg0 weight0 mpg1 weight1, mlabel(rep78)) /// (pcarrow mpg0 weight0 mpg1 weight1), /// legend(off) title("pccapsym || pcarrow") name(g6) graph combine g1 g2 g3 g4 g5 g6, title("Paired coordinates") graph export "paired coordinates.png", replace restore graph drop _all